package com.itlong.cloud.rabbitmq.consumer;

import com.alibaba.fastjson.JSON;
import com.itlong.cloud.POJO.PO.TimeTaskDeadMessageQueuePO;
import com.itlong.cloud.constants.RabbitMQConstant;
import com.itlong.cloud.property.service.IPropertyEquipService;
import com.itlong.cloud.rabbitmq.config.MessageSender;
import com.itlong.cloud.utils.node.JsonUtil;
import com.rabbitmq.client.Channel;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * <desc>
 *      物业接收项目编号，修改该项目下的设备配置人脸识别状态为0的消费者
 * </desc>
 *
 * @createDate 2018/07/03
 */
@Component
public class CloudIntercomChangeFaceConsumer {

    private static final Logger LOG = LoggerFactory.getLogger(CloudIntercomChangeFaceConsumer.class);

    //物业设备服务层
    @Autowired
    IPropertyEquipService iPropertyEquipService;

    //消息队列
    @Autowired
    private MessageSender messageSender;


    /**
     * <desc>
     *      物业接收项目编号，修改该项目下的设备配置人脸识别状态为0的消费者
     * <desc/>
     * @param   message 消息数据
     * @author JuGuang.S
     * @createDate 2018/07/03
     */
    @RabbitListener(queues = RabbitMQConstant.RABBITMQ_CLOUD_TALK_EQUIP_MODIFY_FACE_QUEUE)
    public void process(Message message, Channel channel) throws InterruptedException,IOException {
        byte[] body = message.getBody();
        try {
            TimeTaskDeadMessageQueuePO timeTaskDeadMessageQueuePO = JsonUtil.toObject(new String(body), TimeTaskDeadMessageQueuePO.class);
            String projectId = JSON.parseObject(timeTaskDeadMessageQueuePO.getMessageContent()).getString("data");
            System.err.println(projectId);
            if (StringUtils.isNotBlank(projectId)) {
                iPropertyEquipService.updateEquipFaceStatus(projectId);
            } else {
                // 若项目id为空
                System.err.println("项目id为空");
            }
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
            LOG.error("【RabbitMQ_物业服务-修改物业设备业务消费端】:消费异常",e);
        } finally {
            //手动确认消息消费成功。
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }
    }
}